// credits/pages/exchange-record/index.js
import {
  creditsIconMap
} from '../../config/common'
import Dialog from '../../../libs/@vant/weapp/dialog/dialog';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentValue: '',
    tabs: [{
        label: "全部",
        value: "",
      },
      {
        label: "门店确认",
        value: "1",
      },
      {
        label: "平台确认",
        value: "2",
      },
      {
        label: "审核通过",
        value: "3",
      },
      {
        label: "门店作废",
        value: "4",
      },
      {
        label: "平台作废",
        value: "5",
      },
    ],
    storeId: '',
    show: false,
    detailData: null,
    // item: {
    //   icon: creditsIconMap['3'].icon,
    //   desc: 'safdasf',
    //   create_time: '2023-09-01',
    //   statusName: 'sdfasdf',
    //   file_ids: '1,2,2,2,2'
    // }
  },

  onTabClick(event) {
    this.setData({
      currentValue: event.currentTarget.dataset.value
    }, () => {
      this.refreshCom.refresh()
    })
  },
  requestData() {
    return new Promise((resolve, reject) => {
      wx.ajax
        .post('pageQueryScoreApplyUser.action', {
          type: this.data.currentValue,
          storeId: this.data.storeId,
        }).then(res => {
          res.result.pageList.forEach(item => {
            item.icon = creditsIconMap['3'].icon
            item.statusName = this.tabMap[item.status]
          })
          resolve(res)
        }).catch(reject)
    })
  },
  requestRemove(item) {
    Dialog.confirm({
      title: '删除',
      message: '确认删除？',
    })
      .then(() => {
          wx.ajax.post('delScoreApply.action', item.id).then(res => {
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              })
              this.refreshPage()
          })
      })
      .catch(() => {
        // on cancel
      });
  },
  onAdd() {
    this.setData({
      show: true,
      detailData: null
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  refreshPage() {
      console.log('-----------refreshPage')
    this.refreshCom.refresh()
  },
  onItemEvent(event) {
    const {eventName, item} = event.detail
    
    switch(eventName) {
      case 'itemClick':
        this.setData({
          show: true,
          detailData: item
        })
        break;
      case 'remove':
        this.requestRemove(item);
        break;  
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { tabs } = this.data
    const tabMap = {}
    tabs.forEach(item => {
      tabMap[item.value] = item.label
    })
    this.tabMap = tabMap

    this.refreshCom = this.selectComponent('#listRefresh')
    this.setData({
      storeId: options.storeId,
      currentValue: options.type || '',
      requestFun: this.requestData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})